package icu.lookyousmileface.forkjoin;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ForkJoinTask;
import java.util.stream.LongStream;

/**
 * @author starrysky
 * @title: MainTask
 * @projectName Juc_Pro
 * @description: ForkJoin主任务
 * @date 2021/1/301:31 上午
 */
public class MainTask {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        /**
         * 使用ForkJoin，适合大数据量
         */
        //创建forkjoin池
//        ForkJoinPool forkJoinPool = new ForkJoinPool();
//        //创建自己的ForkJoin计算程序
//        ForkJoinTask forkJoinTask = new MyForkJoinTask(0L, 10_0000_0000L);
//        //提交计算任务
//        ForkJoinTask<Long> submit = forkJoinPool.submit(forkJoinTask);
//        //获得计算的结果
//        Long aLong = submit.get();
//        System.out.println(aLong);

        /**
         * 使用stream并行流,非常快
         */
        long result = LongStream.rangeClosed(0L, 10_0000_0000L).parallel().reduce(0, Long::sum);
        System.out.println(result);

    }
}
